
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.Customer;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class CustomerSessionBean implements CustomerSessionBeanLocal {

    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public void insertCustomer(Customer cu)
    {
           try {
                 em.persist(cu);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Override
    public List<Customer> listAllCustomer() {
        List<Customer> cu = em.createNamedQuery("Customer.findAll").getResultList();
        return cu;
    }

    @Override
    public void deleteCustomer(Customer cu) {

        if (cu != null) {
            em.remove(em.merge(cu));
        } else {
            System.out.println("Not found cu to delete");
        }
    }

    @Override
    public void updateCustomer(String email, String passowrd, String fullName, String address, String phone, int age) {
        Customer cus = em.find(Customer.class, email);
        if (cus != null) {

            cus.setPassword(passowrd);
            cus.setFullName(fullName);
            cus.setAddress(address);
            cus.setPhone(phone);
            cus.setAge(age);

            em.persist(cus);
        } else {
            System.out.println("Not found tcId  to Update");
        }
    }
    @Override
    public List<Customer> searchCustomer(String email)
    {
       List<Customer> cut=null;
       cut=em.createNamedQuery("Customer.findByEmail").setParameter("email", "%"+email+"%").getResultList();
       return cut;
    }

     boolean result=false;
      @Override
    public boolean checkCustomerEmail(String name)
    {
      List<Customer> catList=null;
         catList=em.createQuery("SELECT t FROM Customer t").getResultList();
         for(Customer cat: catList)
         {
           if(cat.getEmail().equals(name))
           {
              result=true;
              break;
           }else
           {
               result=false;
           }
         }
        return  result;
    }
}




    

